package com.kerrykidz.sales.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kerrykidz.common.ORDER_CONSTANTS;
import com.kerrykidz.sales.dao.model.PosOrderEntity;
import com.kerrykidz.sales.dao.model.PosOrderPrdEntity;
import com.kerrykidz.sales.service.IOrderService;
import com.kerrykidz.sales.service.IRetService;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;

/**
 * 退货
 * 
 * @author QUINN
 * 
 */
@RequestMapping("ret")
@Controller
public class RetController {

	@Autowired
	private IRetService retService;

	@Autowired
	private IOrderService orderService;

	@RequestMapping(value = "getRetOrderPrdCount", method = RequestMethod.POST)
	@ResponseBody
	public int getRetOrderPrdCount(@RequestParam("pid") String pid, @RequestParam("oid") String oid) {
		int a = this.retService.getHasRetedOrderPrdCount(oid, pid);
		return a;
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(PosOrderEntity order, @RequestParam("prds") String prds[]) {
		List<PosOrderPrdEntity> products = new ArrayList<PosOrderPrdEntity>();
		UserInfo user = UserUtil.getUserFromSession();
		for (String prdstr : prds) {
			String prd[] = prdstr.split("_");
			if (prd.length == 3) {
				PosOrderPrdEntity o = new PosOrderPrdEntity();
				o.setPkOrderId(order.getPkOrderId());
				o.setType(ORDER_CONSTANTS.ORDER_PRD_TYPE_PRODUCT);
				o.setFkPrdId(prd[0]);
				o.setCount(Integer.parseInt(prd[1]));
				o.setDisPrice(Double.valueOf(prd[2]));
				products.add(o);
			}
		}
		order.setFkBranId(user.getBranId());
		try {
			this.orderService.createRetOrder(order, products, user.getUserId(), user.getAbbrCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/payment/skmx.shtml";
	}
}
